package com.wzh.design.combination.bag1;

/**
 * @description:  组合模式
 * @author: WZH
 * @time: 2021/12/24 10:10
 */

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;

//组合菜单
public class Menu extends Component {

    List<Component> menuComponents = new ArrayList<Component>();
    String name;
    String description;

    public Menu(String name, String description) {
        this.name = name;
        this.description = description;
    }
    @Override
    public void add(Component menuComponent) {
        menuComponents.add(menuComponent);
    }
    @Override
    public void remove(Component menuComponent) {
        menuComponents.remove(menuComponent);
    }
    @Override
    public Component getChild(int i) {
        return menuComponents.get(i);
    }

    @Override
    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    @Override
    public void print(int offset) {
        super.printOffsed(offset);
        System.out.println("{菜单名=" + name + ", 描述=" + description + "}");
        ListIterator<Component> listIterator = menuComponents.listIterator();
        while (listIterator.hasNext()) {
            Component itemComponent = (Component) listIterator.next();
            //递归思想
            itemComponent.print(offset+1);
        }
    }

}

